package com.mattgarner.jaddas.node.command.ddl;

import com.mattgarner.jaddas.dataset.DataSet;
import com.mattgarner.jaddas.node.DataSetManager;
import com.mattgarner.jaddas.node.RemoteClient;
import com.mattgarner.jaddas.node.command.CommandParser;
import com.mattgarner.jaddas.node.net.Protocol;
import com.mattgarner.jaddas.node.net.Worker;

public class CmdDropDataset extends CommandParser {

	public CmdDropDataset(Worker serverWorker, RemoteClient client) {
		super(serverWorker, client);
	}
	
	public final boolean parseCommand(String command) {
		Worker worker = getServerWorker();
		RemoteClient clientConfig = getRemoteClient();
		
		String[] cmd = extractRegexGroups("(?i)^DROP\\s+DATASET\\s+([A-Z0-9_\\.]+?)$", command);
		String dsName = cmd[0];
		if(dsName == null) {
			worker.sendMessage(Protocol.MSG_NAK, "A DataSet must be specified.");
			return true;
		}
		
		DataSetManager dsManager = DataSetManager.getInstance();
		DataSet dataSet = dsManager.getDataSetByName(dsName);
		
		if(dataSet == null) {
			worker.sendMessage(Protocol.MSG_NAK, "DataSet '"+dsName+"' was not found.");
			return true;
		}
		
		dsManager.removeDataSet(dataSet);
		worker.sendMessage(Protocol.MSG_OK, "DataSet '"+dsName+"' successfully dropped. (in "+calculateExecuteTime()+" secs.)");
		return true;
	}

}
